//
//  SpellController.m
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 21/01/2007.
//
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "SpellController.h"
#import "SpellMethods.h"

@implementation SpellController

- (IBAction)autoDetectSpellLevel:(id)sender{
	if([[spellController selectedObjects] count]==0){
		return;
	}
	//check which types are activated.
	NSObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	NSArray *array=[NSArray arrayWithObjects:@"bard",@"wizard",@"cleric",@"druid",@"paladin",@"ranger",@"domain",nil];
	int i;
	for(i=0;i<[array count];i++){
		NSString *str=[array objectAtIndex:i];
		//check if level value is nil and spell type activated.
		if([[spell valueForKey:str] boolValue] && [spell valueForKey:[[NSString alloc] initWithFormat:@"level%@",[str capitalizedString]]]==nil){
			//get the average level value.
			int j;
			int c=0;
			int totalLevel=0;
			for(j=0;j<[array count];j++){
				if(!([spell valueForKey:[[NSString alloc] initWithFormat:@"level%@",[[array objectAtIndex:j] capitalizedString]]]==nil)){
					totalLevel+=[[spell valueForKey:[[NSString alloc] initWithFormat:@"level%@",[[array objectAtIndex:j] capitalizedString]]] intValue];
					c++;
				}
			}
			totalLevel+=c/2;//so it is at least approximately rounded up/down as required.
			if(c!=0){
				totalLevel=totalLevel/c;//now have an approximate value for the level.
				[spell setValue:[NSNumber numberWithInt:totalLevel]
						 forKey:[[NSString alloc] initWithFormat:@"level%@",[str capitalizedString]]];
			}
		}else if(![[spell valueForKey:str] boolValue]){//clear level if deactivated.
			[spell setValue:nil
					 forKey:[[NSString alloc] initWithFormat:@"level%@",[str capitalizedString]]];
		}
	}
}
-(IBAction)durInstantaneousOrPermanentChanged:(id)sender{
	if([sender state]==NSOnState){
		if([[spellController selectedObjects] count]==0){
			//NSLog(@"error, no spell selected SpellController l50");
		}
		//switch on, remove other values.
		NSManagedObject *spell=[[spellController selectedObjects] objectAtIndex:0];
		[spell setValue:nil
				 forKey:@"durBonBonus"];
		[spell setValue:nil
				 forKey:@"durBonNoOfDie"];
		[spell setValue:nil
				 forKey:@"durBonSizeOfDie"];
		[spell setValue:nil
				 forKey:@"durLvlBonus"];
		[spell setValue:nil
				 forKey:@"durLvlNoOfDie"];
		[spell setValue:nil
				 forKey:@"durLvlSizeOfDie"];
		[spell setValue:nil
				 forKey:@"durMaxBonus"];
		[spell setValue:nil
				 forKey:@"durMaxNoOfDie"];
		[spell setValue:nil
				 forKey:@"durMaxSizeOfDie"];
		[spell setValue:[NSNumber numberWithBool:NO]
				 forKey:@"durDischarge"];
		[spell setValue:[NSNumber numberWithBool:NO]
				 forKey:@"durForConc"];
		[spell setValue:nil
				 forKey:@"durMaxConc"];
	}else{
		//going off, do nothing.
	}
}
-(IBAction)displaySpellDurationBox:(id)sender{
	[NSApp beginSheet:durationSheet
	   modalForWindow:mainWindow
		modalDelegate:self
	   didEndSelector:nil
		  contextInfo:NULL];
}
-(IBAction)closeSpellDurationBox:(id)sender{
	if([[spellController selectedObjects] count]==0){
		return;
	}
	[durationSheet orderOut:self];
	[NSApp endSheet:durationSheet];
	//create description.
	NSString *str=@"";
	NSObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	//check measurement unit.
	NSString *measuredIn;
	if([[spell valueForKey:@"durMeasuredIn"] intValue]==0){
		measuredIn=@"rounds";
	}else if([[spell valueForKey:@"durMeasuredIn"] intValue]==1){
		measuredIn=@"minutes";
	}else if([[spell valueForKey:@"durMeasuredIn"] intValue]==2){
		measuredIn=@"hours";
	}else if([[spell valueForKey:@"durMeasuredIn"] intValue]==3){
		measuredIn=@"days";
	}
	//get dice per level
	if([[spell valueForKey:@"durLvlNoOfDie"] intValue]!=0){
		str=[[NSString alloc] initWithFormat:@"%@d%@",[spell valueForKey:@"durLvlNoOfDie"],[spell valueForKey:@"durLvlSizeOfDie"]];
	}
	//get bonus per level.
	if([[spell valueForKey:@"durLvlBonus"] intValue]!=0){
		if([str isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"durLvlBonus"]];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@+%@",str,[spell valueForKey:@"durLvlBonus"]];
		}
	}
	//check if any content
	if(![str isEqualToString:@""]){
		//gets the rate of increase
		int noOfLvls=[[spell valueForKey:@"durPerLvls"] intValue];
		if(noOfLvls==1){
			str=[[NSString alloc] initWithFormat:@"%@ %@/level ",str,measuredIn];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@ %@/%d levels ",str,measuredIn,noOfLvls];
		}
		//try to get maximum.
		NSString *str2=@"";
		if([[spell valueForKey:@"durMaxNoOfDie"] intValue]!=0){
			str2=[[NSString alloc] initWithFormat:@"%@d%@",[spell valueForKey:@"durMaxNoOfDie"],[spell valueForKey:@"durLvlSizeOfDie"]];
		}
		if([[spell valueForKey:@"durMaxBonus"] intValue]!=0){
			if([str2 isEqualToString:@""]){
				str2=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"durMaxBonus"]];
			}else{
				str2=[[NSString alloc] initWithFormat:@"%@+%@",str2,[spell valueForKey:@"durMaxBonus"]];
			}
		}
		if(![str2 isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@(max. %@ %@)",str,str2,measuredIn];
		}
	}
	//add on the fixed bonus.
	NSString *str2=@"";
	if([[spell valueForKey:@"durBonNoOfDie"] intValue]!=0){
		str2=[[NSString alloc] initWithFormat:@"%@d%@ ",[spell valueForKey:@"durBonNoOfDie"],[spell valueForKey:@"durBonSizeOfDie"]];
	}
	if([[spell valueForKey:@"durBonBonus"] intValue]!=0){
		if([str2 isEqualToString:@""]){
			str2=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"durBonBonus"]];
		}else{
			str2=[[NSString alloc] initWithFormat:@"%@+%@",str2,[spell valueForKey:@"durBonBonus"]];
		}
	}
	if(![str2 isEqualToString:@""]){
		if([str isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@ %@",str2,measuredIn];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@ + %@ %@",str,str2,measuredIn];
		}
	}
	//add Conc bonus.
	if([[spell valueForKey:@"durForConc"] boolValue]){
		if([str isEqualToString:@""]){
			str=@"concentration ";
		}else{
			str=[[NSString alloc] initWithFormat:@"%@+conc. ",str];
		}
		if([[spell valueForKey:@"durMaxConc"] intValue]!=0){
			str=[[NSString alloc] initWithFormat:@"%@(max. %@ %@) ",str,[spell valueForKey:@"durMaxConc"],measuredIn];
		}
	}
	if(![str isEqualToString:@""] && [[spell valueForKey:@"durInstantaneous"] boolValue]){
		[spell setValue:[NSNumber numberWithBool:NO]
				 forKey:@"durInstantaneous"];
	}
	//check for dischargable.
	if([[spell valueForKey:@"durDischarge"] boolValue]){
		str=[[NSString alloc] initWithFormat:@"%@ (D)",str];
	}
	if([[spell valueForKey:@"durInstantaneous"] boolValue]){
		[spell setValue:@"Instantaneous"
				 forKey:@"durDescription"];
		//[durationDescription setStringValue:@"Instantaneous"];
	}else if([[spell valueForKey:@"durPermanent"] boolValue]){
		[spell setValue:@"Permanent"
				 forKey:@"durDescription"];
	}else{
		[spell setValue:str
				 forKey:@"durDescription"];
		//[durationDescription setStringValue:str];
	}
}
-(IBAction)displayNoOfAttacksBox:(id)sender{
	[NSApp beginSheet:noOfAttacksSheet
	   modalForWindow:mainWindow
		modalDelegate:self
	   didEndSelector:nil
		  contextInfo:NULL];
}
-(IBAction)closeNoOfAttacksBox:(id)sender{
	if([[spellController selectedObjects] count]==0){
		return;
	}
	[noOfAttacksSheet orderOut:self];
	[NSApp endSheet:noOfAttacksSheet];
	//create description.
	NSString *str=@"";
	NSObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	//get dice per level
	if([[spell valueForKey:@"noaLvlNoOfDie"] intValue]!=0){
		str=[[NSString alloc] initWithFormat:@"%@d%@",[spell valueForKey:@"noaLvlNoOfDie"],[spell valueForKey:@"noaLvlSizeOfDie"]];
	}
	//get bonus per level.
	if([[spell valueForKey:@"noaLvlBonus"] intValue]!=0){
		if([str isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"noaLvlBonus"]];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@+%@",str,[spell valueForKey:@"noaLvlBonus"]];
		}
	}
	//check if any content
	if(![str isEqualToString:@""]){
		//gets the rate of increase
		int noOfLvls=[[spell valueForKey:@"noaPerLvls"] intValue];
		if(noOfLvls==1){
			str=[[NSString alloc] initWithFormat:@"%@/level ",str];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@/%d levels ",str,noOfLvls];
		}
		//try to get maximum.
		NSString *str2=@"";
		if([[spell valueForKey:@"noaMaxNoOfDie"] intValue]!=0){
			str2=[[NSString alloc] initWithFormat:@"%@d%@",[spell valueForKey:@"noaMaxNoOfDie"],[spell valueForKey:@"noaLvlSizeOfDie"]];
		}
		if([[spell valueForKey:@"noaMaxBonus"] intValue]!=0){
			if([str2 isEqualToString:@""]){
				str2=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"noaMaxBonus"]];
			}else{
				str2=[[NSString alloc] initWithFormat:@"%@+%@",str2,[spell valueForKey:@"noaMaxBonus"]];
			}
		}
		if(![str2 isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@(max. %@)",str,str2];
		}
	}
	//add on the fixed bonus.
	NSString *str2=@"";
	if([[spell valueForKey:@"noaBonNoOfDie"] intValue]!=0){
		str2=[[NSString alloc] initWithFormat:@"%@d%@ ",[spell valueForKey:@"noaBonNoOfDie"],[spell valueForKey:@"noaBonSizeOfDie"]];
	}
	if([[spell valueForKey:@"noaBonBonus"] intValue]!=0){
		if([str2 isEqualToString:@""]){
			str2=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"noaBonBonus"]];
		}else{
			str2=[[NSString alloc] initWithFormat:@"%@+%@",str2,[spell valueForKey:@"noaBonBonus"]];
		}
	}
	if(![str2 isEqualToString:@""]){
		if([str isEqualToString:@""]){
			str=str2;
		}else{
			str=[[NSString alloc] initWithFormat:@"%@ + %@",str,str2];
		}
	}
	if([str isEqualToString:@""]){
		[spell setValue:@"None"
				 forKey:@"noaDesc"];
	}else{
		[spell setValue:str
				 forKey:@"noaDesc"];
	}
}
-(IBAction)displayDamageBox:(id)sender{
	[NSApp beginSheet:damageSheet
	   modalForWindow:mainWindow
		modalDelegate:self
	   didEndSelector:nil
		  contextInfo:NULL];
}
-(IBAction)closeDamageBox:(id)sender{
	if([[spellController selectedObjects] count]==0){
		return;
	}
	[damageSheet orderOut:self];
	[NSApp endSheet:damageSheet];
	//create description.
	NSString *str=@"";
	NSObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	//get dice per level
	if([[spell valueForKey:@"damLvlNoOfDie"] intValue]!=0){
		str=[[NSString alloc] initWithFormat:@"%@d%@",[spell valueForKey:@"damLvlNoOfDie"],[spell valueForKey:@"damLvlSizeOfDie"]];
	}
	//get bonus per level.
	if([[spell valueForKey:@"damLvlBonus"] intValue]!=0){
		if([str isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"damLvlBonus"]];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@+%@",str,[spell valueForKey:@"damLvlBonus"]];
		}
	}
	//check if any content
	if(![str isEqualToString:@""]){
		//gets the rate of increase
		int noOfLvls=[[spell valueForKey:@"damPerLvls"] intValue];
		if(noOfLvls==1){
			str=[[NSString alloc] initWithFormat:@"%@/level ",str];
		}else{
			str=[[NSString alloc] initWithFormat:@"%@/%d levels ",str,noOfLvls];
		}
		//try to get maximum.
		NSString *str2=@"";
		if([[spell valueForKey:@"damMaxNoOfDie"] intValue]!=0){
			str2=[[NSString alloc] initWithFormat:@"%@d%@",[spell valueForKey:@"damMaxNoOfDie"],[spell valueForKey:@"damLvlSizeOfDie"]];
		}
		if([[spell valueForKey:@"damMaxBonus"] intValue]!=0){
			if([str2 isEqualToString:@""]){
				str2=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"damMaxBonus"]];
			}else{
				str2=[[NSString alloc] initWithFormat:@"%@+%@",str2,[spell valueForKey:@"damMaxBonus"]];
			}
		}
		if(![str2 isEqualToString:@""]){
			str=[[NSString alloc] initWithFormat:@"%@(max. %@)",str,str2];
		}
	}
	//add on the fixed bonus.
	NSString *str2=@"";
	if([[spell valueForKey:@"damBonNoOfDie"] intValue]!=0){
		str2=[[NSString alloc] initWithFormat:@"%@d%@ ",[spell valueForKey:@"damBonNoOfDie"],[spell valueForKey:@"damBonSizeOfDie"]];
	}
	if([[spell valueForKey:@"damBonBonus"] intValue]!=0){
		if([str2 isEqualToString:@""]){
			str2=[[NSString alloc] initWithFormat:@"%@",[spell valueForKey:@"damBonBonus"]];
		}else{
			str2=[[NSString alloc] initWithFormat:@"%@+%@",str2,[spell valueForKey:@"damBonBonus"]];
		}
	}
	if(![str2 isEqualToString:@""]){
		if([str isEqualToString:@""]){
			str=str2;
		}else{
			str=[[NSString alloc] initWithFormat:@"%@ + %@",str,str2];
		}
	}
	if([str isEqualToString:@""]){
		[spell setValue:@"None"
				 forKey:@"damDescription"];
	}else{
		[spell setValue:str
				 forKey:@"damDescription"];
	}
}
-(IBAction)displayRangeBox:(id)sender{
	if([[spellController selectedObjects] count]==0){
		return;
	}
	//NSLog(@"hi!");
	//check whether a pre-defined range has been chosen.
	NSManagedObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	[spell setValue:[sender title]
			 forKey:@"range"];
	//NSLog(@"spell range value=%@",[spell valueForKey:@"range"]);
	if([[spell valueForKey:@"range"] isEqualToString:@"Personal"] || [[spell valueForKey:@"range"] isEqualToString:@"Touch"]){
		[spell setValue:nil
				 forKey:@"ranLvlNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranLvlSizeOfDie"];
		[spell setValue:nil
				 forKey:@"ranLvlBonus"];
		[spell setValue:nil
				 forKey:@"ranMaxNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranMaxBonus"];
		[spell setValue:nil
				 forKey:@"ranBonNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranBonSizeOfDie"];
		[spell setValue:nil
				 forKey:@"ranBonBonus"];
		[spell setValue:nil
				 forKey:@"ranPerLvls"];
		[spell setValue:[NSNumber numberWithInt:0]
				 forKey:@"ranMeasuredIn"];
		//no change required.
	}else if([[spell valueForKey:@"range"] isEqualToString:@"Close"]){
		[spell setValue:nil
				 forKey:@"ranLvlNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranLvlSizeOfDie"];
		[spell setValue:[NSNumber numberWithInt:5]
				 forKey:@"ranLvlBonus"];
		[spell setValue:nil
				 forKey:@"ranMaxNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranMaxBonus"];
		[spell setValue:nil
				 forKey:@"ranBonNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranBonSizeOfDie"];
		[spell setValue:[NSNumber numberWithInt:25]
				 forKey:@"ranBonBonus"];
		[spell setValue:[NSNumber numberWithInt:2]
				 forKey:@"ranPerLvls"];
		[spell setValue:[NSNumber numberWithInt:0]
				 forKey:@"ranMeasuredIn"];
	}else if([[spell valueForKey:@"range"] isEqualToString:@"Medium"]){
		[spell setValue:nil
				 forKey:@"ranLvlNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranLvlSizeOfDie"];
		[spell setValue:[NSNumber numberWithInt:10]
				 forKey:@"ranLvlBonus"];
		[spell setValue:nil
				 forKey:@"ranMaxNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranMaxBonus"];
		[spell setValue:nil
				 forKey:@"ranBonNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranBonSizeOfDie"];
		[spell setValue:[NSNumber numberWithInt:100]
				 forKey:@"ranBonBonus"];
		[spell setValue:[NSNumber numberWithInt:1]
				 forKey:@"ranPerLvls"];
		[spell setValue:[NSNumber numberWithInt:0]
				 forKey:@"ranMeasuredIn"];
	}else if([[spell valueForKey:@"range"] isEqualToString:@"Long"]){
		[spell setValue:nil
				 forKey:@"ranLvlNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranLvlSizeOfDie"];
		[spell setValue:[NSNumber numberWithInt:40]
				 forKey:@"ranLvlBonus"];
		[spell setValue:nil
				 forKey:@"ranMaxNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranMaxBonus"];
		[spell setValue:nil
				 forKey:@"ranBonNoOfDie"];
		[spell setValue:nil
				 forKey:@"ranBonSizeOfDie"];
		[spell setValue:[NSNumber numberWithInt:400]
				 forKey:@"ranBonBonus"];
		[spell setValue:[NSNumber numberWithInt:1]
				 forKey:@"ranPerLvls"];
		[spell setValue:[NSNumber numberWithInt:0]
				 forKey:@"ranMeasuredIn"];
	}
	if([[spell valueForKey:@"range"] isEqualToString:@"Custom"]){
		//NSLog(@"loading sheet");
		[NSApp beginSheet:rangeSheet
		   modalForWindow:mainWindow
			modalDelegate:self
		   didEndSelector:nil
			  contextInfo:NULL];
	}else{
		NSLog(@"processing r description");
		[SpellMethods processRangeDescriptionForSpell:spell];
	}
	//NSLog(@"finished");
}
-(IBAction)closeRangeBox:(id)sender{
	[rangeSheet orderOut:self];
	[NSApp endSheet:rangeSheet];
	if([[spellController selectedObjects] count]==0){
		NSLog(@"no spell selected");
		return;
	}
	//NSLog(@"hi!");
	//check whether a pre-defined range has been chosen.
	NSManagedObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	[SpellMethods processRangeDescriptionForSpell:spell];
}
-(IBAction)spellAbilityChosen:(id)sender{
	if([[spellAbilKey1 titleOfSelectedItem] isEqualToString:@"Hasted"] || [[spellAbilKey1 titleOfSelectedItem] isEqualToString:@"Slowed"]){
		[spellAbilDie1 setEnabled:NO];
		[spellAbilFixed1 setEnabled:NO];
	}else{
		[spellAbilDie1 setEnabled:YES];
		[spellAbilFixed1 setEnabled:YES];
	}
	if([[spellAbilKey2 titleOfSelectedItem] isEqualToString:@"Hasted"] || [[spellAbilKey2 titleOfSelectedItem] isEqualToString:@"Slowed"]){
		[spellAbilDie2 setEnabled:NO];
		[spellAbilFixed2 setEnabled:NO];
	}else{
		[spellAbilDie2 setEnabled:YES];
		[spellAbilFixed2 setEnabled:YES];
	}
	if([[spellAbilKey3 titleOfSelectedItem] isEqualToString:@"Hasted"] || [[spellAbilKey3 titleOfSelectedItem] isEqualToString:@"Slowed"]){
		[spellAbilDie3 setEnabled:NO];
		[spellAbilFixed3 setEnabled:NO];
	}else{
		[spellAbilDie3 setEnabled:YES];
		[spellAbilFixed3 setEnabled:YES];
	}
	if([[spellAbilKey4 titleOfSelectedItem] isEqualToString:@"Hasted"] || [[spellAbilKey4 titleOfSelectedItem] isEqualToString:@"Slowed"]){
		[spellAbilDie4 setEnabled:NO];
		[spellAbilFixed4 setEnabled:NO];
	}else{
		[spellAbilDie4 setEnabled:YES];
		[spellAbilFixed4 setEnabled:YES];
	}
}
-(IBAction)setUpAreaOfEffectLabel:(id)sender{
	NSManagedObject *spell=[[spellController selectedObjects] objectAtIndex:0];
	if([[spell valueForKey:@"areaOfEffect"] intValue]==1){
		[areaOfEffectLabel setStringValue:@"radius"];
	}else{
		[areaOfEffectLabel setStringValue:@"length"];
	}
}
@end
